package model;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

import view.SquareRep;

/**
 * Represents a type of square, e.g. a grass tile, or a dungeon floor, or a
 * dungeon wall", etc.
 */
public class SquareType {
	/**
	 * The name of this type of square, e.g. "grass". Should be globally unique,
	 * and can be used to lookup this specific square type.
	 */
	private final String name;
	/**
	 * The icon used to draw this square.
	 */
	private final Color icon; //TODO Should be ImageIcon later on, Color just as of proof of concept. Might also be SpriteList, in the case of animated tiles like water or lava.
	/**
	 * If true, then the player (and most enemies) cannot walk through this tile.
	 */
	private final boolean isBlocking;
	/**
	 * True if squares of this type should start with a pellet, false otherwise.
	 */
	private final boolean hasPellet;

	/**
	 * Constructs a new SquareType.
	 * 
	 * @param icon
	 *          the icon used to draw this square.
	 * @param name
	 *          the name of this square (should be globally unique)
	 * @param isBlocking
	 *          true if the player can freely walk through this tile type, false
	 *          otherwise.
	 * @param hasPellet
	 *          true if squares of this type should start with a pellet, false
	 *          otherwise.
	 */
	public SquareType(final Color icon, final String name,
			final boolean isBlocking, final boolean hasPellet) {
		this.icon = icon;
		this.name = name;
		this.isBlocking = isBlocking;
		this.hasPellet = hasPellet;
	}

	/**
	 * Returns the icon used to draw this square.
	 * 
	 * @return the icon used to draw this square.
	 */
	public ImageIcon getIcon() {
		return new ImageIcon(){
			public void paintIcon(Component c, Graphics g, int x, int y){
				g.setColor(icon);
				g.fillRect(x, y, SquareRep.WIDTH, SquareRep.HEIGHT);
			}
			public Image getImage(){
			    BufferedImage buff = new BufferedImage(SquareRep.WIDTH, SquareRep.HEIGHT, BufferedImage.TYPE_INT_ARGB);
			    Graphics g = buff.createGraphics();
			    g.setColor(icon);
			    g.fillRect(0, 0, SquareRep.WIDTH, SquareRep.HEIGHT);
			    g.dispose();
			    return buff;
			}
		};
	}

	/**
	 * Returns true if the player (and most enemies) cannot walk through this
	 * tile, false otherwise.
	 * 
	 * @return true if the player (and most enemies) cannot walk through this
	 *         tile, false otherwise.
	 */
	public boolean isBlocking() {
		return this.isBlocking;
	}

	/**
	 * Returns true if squares of this type should start with a pellet, false
	 * otherwise.
	 * 
	 * @return true if squares of this type should start with a pellet, false
	 *         otherwise.
	 */
	public boolean hasPellet() {
		return this.hasPellet;
	}
}
